package cn.com.enersun.iec61850.web.controller;

import cn.com.enersun.iec61850.integratedDataProcessing.*;
import cn.com.enersun.iec61850.web.domain.DeviceList;
import cn.com.enersun.iec61850.web.domain.User;
import cn.com.enersun.iec61850.web.mapper.DeviceListMapper;
import cn.com.enersun.iec61850.web.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lionel on 2017/4/19.
 */
@RestController
@RequestMapping("/iec61850")
public class SampleController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TableService tableService;
    @Autowired
    private DeviceListMapper deviceListMapper;

    @RequestMapping(value = "/sample")
    public User sample() {
        ModelAndView mv = new ModelAndView("index");
        User user = userMapper.findByName("liuxi");
        mv.addObject("msg", "spring boot例子");
        mv.addObject("user", user);
        return user;
    }
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        return new ModelAndView("hello");
    }

    @RequestMapping(value = "/deviceLists")
    public Map<String, Object>  deviceLists(){
        Map<String, Object> response = new HashMap<String, Object>();

        try {
            List<DeviceList> devLists = deviceListMapper.selectDevices();
            response.put("success", true);
            response.put("data", devLists);
        }catch (Exception ex){
            response.put("success", false);
            response.put("msg", "设备列表获取错误");
        }
        return response;
    }

    @RequestMapping(value= "/easyTree")
    public Map<String, Object> easyTree(String type, String ip, String path) {
        Map<String, Object> response = new HashMap<String, Object>();

        if((type == null) || type.equals("none")) {
            try {
                EasyTreeJsonObject easyTreeJsonObject = new EasyTreeJsonObject(IntegratedDataProcessing.clientServerModels.get(ip));
                response.put("success", true);
                response.put("data", easyTreeJsonObject);
            }catch (Exception ex) {
                response.put("success", false);
                response.put("msg", "设备数据模型获取错误");
            }
        }else if(type.equals("byNet")) {
            try {
                EasyTreeJsonObject easyTreeJsonObject = new EasyTreeJsonObject(ip);
                response.put("success", true);
                response.put("data", easyTreeJsonObject);
            }catch (Exception ex) {
                response.put("success", false);
                response.put("msg", "设备数据模型获取错误");
            }
        }else if(type.equals("byScl")) {
            try {
                EasyTreeJsonObject easyTreeJsonObject = new EasyTreeJsonObject(ip, path);
                response.put("success", true);
                response.put("data", easyTreeJsonObject);
            }catch (Exception ex) {
                response.put("success", false);
                response.put("msg", "设备数据模型获取错误");
            }
        }
        return response;
    }

    @RequestMapping(value= "/tableList")
    public Map<String, Object> tableList(String ip, String id) {
        Map<String, Object> response = new HashMap<String, Object>();

        try {
            tableService.setServerModel(IntegratedDataProcessing.clientServerModels.get(ip));
            response.put("success", true);
            response.put("data", tableService.getTableList(id));
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("msg", "设备数据模型获取错误");
        }
        return response;
    }

    @RequestMapping(value= "/tableListDetail")
    public Map<String, Object> tableListDetail(String ip, String id) {
        Map<String, Object> response = new HashMap<String, Object>();

        try {
            tableService.setServerModel(IntegratedDataProcessing.clientServerModels.get(ip));
            Map<String,List<TableListDetail>> detail = tableService.getTableListDetail(id);
            response.put("success", true);
            response.put("data", detail);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("msg", "设备数据模型获取错误");
        }
        return response;
    }

    @RequestMapping(value= "/table")
    public Map<String, Object> table(String ip, String id, String disType) {
        Map<String, Object> response = new HashMap<String, Object>();

        try {
            tableService.setServerModel(IntegratedDataProcessing.clientServerModels.get(ip));
            response.put("success", true);
            response.put("data", tableService.getTable(id, disType));
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("msg", "设备数据模型获取错误");
        }

        return response;
    }

    @RequestMapping(value= "/tableParent")
    public Object tableParent(String ip, String id, String disType) {
        Map<String, Object> response = new HashMap<String, Object>();

        try {
            tableService.setServerModel(IntegratedDataProcessing.clientServerModels.get(ip));
            Object object = tableService.getTableParent(id, disType);
            response.put("success", true);
            response.put("data", object);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("msg", "设备数据模型获取错误");
        }
        return response;
    }
}
